package com.micromall.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.micromall.common.base.BaseController;
import com.micromall.common.utils.*;
import com.micromall.model.BusiProduct;
import com.micromall.service.IBusiProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 产品表  前端控制器
 * </p>
 * @author Dante Fung
 * @since 2017-07-26
 */
@Controller
@RequestMapping("/busiProduct")
public class BusiProductController extends BaseController {
    
    @Autowired private IBusiProductService busiProductService;
    
    @GetMapping("/manager")
    public String manager() {
        return "admin/busiProduct/busiProductList";
    }


    @RequestMapping("/dataGrid")
    @ResponseBody
    public PageInfo dataGrid(BusiProduct busiProduct, Integer page, Integer rows, String sort,String order) {
        EntityWrapper<BusiProduct> ew = new EntityWrapper<BusiProduct>();
        ew.setEntity(busiProduct);
        Page<BusiProduct> pages = getPage(page, rows, sort, order);
        pages = busiProductService.selectPage(pages,ew);
        String aa = pageToPageInfo(pages).toString();
        return pageToPageInfo(pages);
    }
    
    /**
     * 添加页面
     * @return
     */
    @GetMapping("/addPage")
    public String addPage() {
        return "admin/busiProduct/busiProductAdd";
    }

    /**
     * 添加
     * @param 
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Object add(BusiProduct busiProduct) {
        busiProduct.setCreateTime(DateUtil.convertDateToStr(new Date(), DateUtil.DEFAULT_DATE_FORMAT));
        busiProduct.setDataStatus(Dict.YesOrNo.Yes.getCode());
        boolean b = busiProductService.insert(busiProduct);
        if (b) {
            return renderSuccess("添加成功！");
        } else {
            return renderError("添加失败！");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Object delete(String id) {
        BusiProduct busiProduct = new BusiProduct();
        busiProduct.setId(id);
        busiProduct.setDataStatus(Dict.YesOrNo.No.getCode());
        boolean b = busiProductService.updateById(busiProduct);
        if (b) {
            return renderSuccess("删除成功！");
        } else {
            return renderError("删除失败！");
        }
    }

    /**
     * 编辑
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/editPage")
    public String editPage(Model model, Long id) {
        BusiProduct busiProduct = busiProductService.selectById(id);
        model.addAttribute("busiProduct", busiProduct);
        return "admin/busiProduct/busiProductEdit";
    }

    /**
     * 编辑
     * @param 
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(BusiProduct busiProduct) {
        boolean b = busiProductService.updateById(busiProduct);
        if (b) {
            return renderSuccess("编辑成功！");
        } else {
            return renderError("编辑失败！");
        }
    }

    /**
     * 跳转到商品详情页
     * @return
     */
    @RequestMapping("/do_detail_Page")
    public String do_detail_Page(HttpServletRequest request, String id){
        EntityWrapper<BusiProduct> ew = new EntityWrapper<BusiProduct>();
        BusiProduct bp = new BusiProduct();
        if(StringUtils.isNotBlank(id)){
            bp.setId(id);
        }
        ew.setEntity(bp);
        BusiProduct busiProduct = busiProductService.selectOne(ew);
        String orderToken = MD5Util.getMD5String(ValidateUtil.genSmsValidateCode());
        request.getSession().setAttribute("orderToken", orderToken);
        request.setAttribute("orderToken", orderToken);
        request.setAttribute("busiProduct",busiProduct);
        return "/detail";
    }

    /**
     * 跳转到商品搜索页
     * @return
     */
    @RequestMapping("/doSearchPage")
    public String doSearchPage(HttpServletRequest request,String searchName,String type){
        EntityWrapper<BusiProduct> ew = new EntityWrapper<BusiProduct>();
        BusiProduct bp = new BusiProduct();
        bp.setDataStatus(Dict.YesOrNo.Yes.getCode());
        bp.setPublishStatus(Dict.YesOrNo.Yes.getCode());
        ew.setEntity(bp);
        List<BusiProduct> busiProductList = new ArrayList<BusiProduct>();
        if(StringUtils.isNotBlank(searchName) && StringUtils.isNotBlank(type)){
            //页面点击搜索,名称搜索和排序方式
            try{
                searchName = URLDecoder.decode(searchName, "UTF-8");
                type = URLDecoder.decode(type, "UTF-8");
                busiProductList = busiProductService.searchList(searchName,type);
            }catch (UnsupportedEncodingException e){
                e.printStackTrace();
            }
            request.setAttribute("type",type);
            request.setAttribute("searchName",searchName);
            request.setAttribute("busiProductList",busiProductList);
            logger.info("searchName:[{}]-type[{}]-busiProductList[{}]",searchName,type,busiProductList);
        }else if(StringUtils.isBlank(searchName) && StringUtils.isNotBlank(type)){
            //没有搜索默认排序方式
            try{
                type = URLDecoder.decode(type, "UTF-8");
                busiProductList = busiProductService.searchList(searchName,type);
            }catch (UnsupportedEncodingException e){
                e.printStackTrace();
            }
            request.setAttribute("type",type);
            request.setAttribute("busiProductList",busiProductList);
            logger.info("type:[{}]-busiProductList[{}]",type,busiProductList);
        }else{
            //从首页跳转到产品搜索
            busiProductList =  busiProductService.selectList(ew);
            request.setAttribute("busiProductList",busiProductList);
            logger.info("初次跳转搜索页面: -busiProductList[{}]",busiProductList);
        }
        return "/search";
    }

}
